package controller

import (
	"fmt"
	"store/config"
	"store/middleware"

	"github.com/cihub/seelog"
	"github.com/gin-gonic/gin"
)

var engine *gin.Engine

// InitRouter 初始化rest路由
func InitRouter() {
	gin.SetMode(gin.ReleaseMode)
	gin.DefaultWriter = nil
	r := gin.New()
	engine = r
	r.Use(middleware.ServerErr(), middleware.LoggerToSeelog(), middleware.Cors())

	//api file
	apiFileGroup := r.Group("api/file")
	{
		apiFileGroup.POST("upload", FileUpload)
		apiFileGroup.GET("download", FileDownload)
		apiFileGroup.DELETE("delete", FileDelete)
	}

}

// StartHTTP 启动http服务
func StartHTTP() {
	restConfig := config.GetConfig().Rest
	seelog.Infof("http 服务监听 %d", restConfig.Port)
	err := engine.Run(fmt.Sprintf(":%d", restConfig.Port))
	if nil != err {
		_ = seelog.Errorf("http服务启动失败,%s", err)
		panic(err)
	}
}
